﻿Public Class DatHangDA
    Private cn As DatabaseConnect
    Private sql As String

    Public Sub New()
        cn = New DatabaseConnect()
    End Sub

    Public Function InPhieuDH(ByVal dhDT As DatHangDT)
        sql = "INSERT INTO SanPham_DatHang(MaPhieuDH,ngayDH,MaNV,MaKH,Tinhtrang,ngayhengiao,tongtien) VALUES('" + dhDT.MaDatHang + "', '" + dhDT.NgayDatHang + "', '" + dhDT.MaNV + "', '" + dhDT.MaKH + "',N'" + dhDT.TinhTrang + "', '" + dhDT.NgayHenGiao + "', " & dhDT.TongTien & ")"
        Return cn.Execute(sql)
    End Function

    Public Function UpTongTien(ByVal dhDT As DatHangDT)
        sql = "UPDATE SanPham_DatHang SET tongtien = " & dhDT.TongTien & " WHERE MaPhieuDH = '" + dhDT.MaDatHang + "'"
        Return cn.Execute(sql)
    End Function

    Public Function UpNgayHenGiao(ByVal dhDT As DatHangDT)
        sql = "UPDATE SanPham_DatHang SET ngayhengiao = '" + dhDT.NgayHenGiao + "' WHERE MaPhieuDH = '" + dhDT.MaDatHang + "'"
        Return cn.Execute(sql)
    End Function

    Public Function GetData(ByVal dieukien As String) As List(Of DatHangDT)
        Dim dh As DatHangDT
        Dim list As New List(Of DatHangDT)
        Dim dt As New DataTable
        dt = cn.GetData("SELECT * FROM SanPham_DatHang " + dieukien)

        For i As Integer = 0 To dt.Rows.Count - 1
            Dim ma As String = dt.Rows(i)("MaPhieuDH").ToString()
            Dim ngaydat As Date = Convert.ToDateTime(dt.Rows(i)("ngayDH"))
            Dim manv As String = dt.Rows(i)("MaNV").ToString()
            Dim makh As String = dt.Rows(i)("MaKH").ToString()
            Dim tinhtrang As String = dt.Rows(i)("Tinhtrang").ToString()
            Dim ngayhengiao As Date = Convert.ToDateTime(dt.Rows(i)("ngayhengiao"))
            Dim tongtien As Double = Convert.ToDouble(dt.Rows(i)("tongtien"))
            '-----'
            dh = New DatHangDT(ma, ngaydat, ngayhengiao, manv, makh, tinhtrang, tongtien)
            list.Add(dh)
        Next
        Return list
    End Function

    Function GetID() As Integer
        Dim dt As New DataTable
        Dim id As Integer
        dt = cn.GetData("SELECT * FROM SanPham_DatHang")
        If dt.Rows.Count > 0 Then
            id = Convert.ToInt32(dt.Rows(dt.Rows.Count - 1).Item("Id"))
        Else
            id = 1
        End If
        Return id
    End Function

    Public Function GetInserted() As String
        Dim dt As New DataTable
        Dim ma As String
        dt = cn.GetData("SELECT * FROM SanPham_DatHang")
        ma = dt.Rows(dt.Rows.Count - 1)("MaPhieuDH").ToString()
        Return ma
    End Function
End Class
